import UIKit

class RichTextView: UIView, UITextViewDelegate {
    private var sharedRichTextView: UITextView!
    private var checkBtn: UIButton!
    private var myBlock: ((String, NSObject?) -> Void)?
    
    init(completion: @escaping (String, NSObject?) -> Void) {
        super.init(frame: .zero)
        myBlock = completion
        setupUI()
        addNotificationCenter()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        checkBtn = UIButton()
        checkBtn?.setImage(UIImage(named: "check-normal"), for: .normal)
        checkBtn?.setImage(UIImage(named: "check-select"), for: .selected)
//        checkBtn?.addAction(self, for: .touchUpInside)
        checkBtn.addTarget(self, action: #selector(check), for: .touchUpInside)
        sharedRichTextView = UITextView()
        sharedRichTextView.isEditable = false
        sharedRichTextView.isScrollEnabled = false
        sharedRichTextView.delegate = self
        sharedRichTextView.textContainerInset = UIEdgeInsets.zero
        addSubview(checkBtn!)
        addSubview(sharedRichTextView!)
        checkBtn.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.width.equalTo(20)
        }
        sharedRichTextView.snp.makeConstraints { make in
            make.left.equalTo(checkBtn.snp.right).offset(5)
            make.centerY.equalToSuperview()
        }
    }
    
    private func addNotificationCenter() {
        NotificationCenter.default.addObserver(self, selector: #selector(printNotificationCenter), name: Notification.Name("YourNotificationName"), object: nil)
    }
    
    @objc private func printNotificationCenter() {
        print("收到通知了")
    }
    
    private func configAttText(configArr: [RichModel],
                               withNormalColor: UIColor,
                               withNormalFont: UIFont,
                               withSelectColor: UIColor,
                               withSelectFont: UIFont) {
        let normalAtt = [NSAttributedString.Key.font: withNormalFont, NSAttributedString.Key.foregroundColor: withNormalColor]
        let specAtt = [NSAttributedString.Key.font: withSelectFont, NSAttributedString.Key.foregroundColor: withSelectColor]
        
        let attStr = NSMutableAttributedString()
        
        for model in configArr {
            let type = model.type
            
            switch type {
            case .Normal:
                let normalStr = NSAttributedString(string: model.text, attributes: normalAtt)
                attStr.append(normalStr)
                
            case .Highlight:
                let selectStr = NSAttributedString(string: model.text, attributes: specAtt)
                attStr.append(selectStr)
                let value = "\(String(describing: model.methodName ?? ""))"
                attStr.addAttributes([NSAttributedString.Key.link: value], range: NSRange(location: attStr.length - selectStr.length, length: selectStr.length))
                
            }
        }
        
        sharedRichTextView.attributedText = attStr
    }
    
    
    @objc private func check(_ btn: UIButton) {
        btn.isSelected = !btn.isSelected
        myBlock?("checked", btn.isSelected as NSObject?)
    }
    
    
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        guard let scheme = URL.scheme, !scheme.isEmpty else {
            return true
        }
        myBlock?(scheme, nil)
        return false
    }
}

extension RichTextView {
    enum RichType {
        case Normal
        case Highlight
    }
    
    struct RichModel {
        let text: String
        let type: RichType
        let methodName: String?
        init(text: String, type: RichType, methodName: String?) {
            self.text = text
            self.type = type
            self.methodName = methodName
        }
    }
}
